import { Model } from '../model.js';
var model = new Model();
var area = require('../../../utils/area.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    orderNo:0,
    areaList:area.default,
    currency:'',
    province:'',
    city:'',
    area:'',
    area_address: '',
    user:{
      address: '',
    },
    product:{},
    submit_flag: false,   //提交标识
    number:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var number=0
    var id=0
    var orderNo=0
    if(options.id){
      id=options.id
      number=options.number
      this.getPayDetail(id)
    }
    
    if(options.orderNo){
      orderNo=options.orderNo
      this.getOrderInfo(orderNo)
    }

    this.setData({number:number,id:id,orderNo})

  },

  /**
   * 获取下单信息
   * @param {*} id 
   */
  getPayDetail(id){
    model.getIntegralPayDetail(id,(res)=>{
      var user = res.data.user;
      let area_address=res.data.area_address
      if(user.area!=null && user.province!=null && user.city!=null){
        area_address = user.province + '-' + user.city + '-' + user.area;  //合并地区
        this.data.province = user.province;
        this.data.city = user.city;
        this.data.area = user.area;
      }
      
      user.address == null ? user.address = this.data.user.address :'';
      this.data.user = user;
      this.data.product = res.data.product;
      var data={   // 取出数据库中数据
        product: res.data.product,
        user:user,
        area_address
      };
      this.setData(data)
    })
  },

  /**
   * 获取订单详情
   * @param {*} orderNo 
   */
  getOrderInfo(orderNo){
    model.getIntegralOrderInfo(orderNo,(res)=>{
      let resUser=res.data.user
      let area_address=resUser.province + '-' + resUser.city + '-' + resUser.area;
      let data={
        product:res.data.product,
        user:resUser,
        number:res.data.product.number,
        area_address
      }
      this.setData(data)
    })
  },

  // 增加数量
  clickNumber(e) {
    let number=e.detail
    this.number = number
    this.setData({
      number: number
    })
  },
  // 回首页
  clickHome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 选择地址
  changeArea:function(){
    this.setData({ select_address:true })
  },
  // 关闭地址选择
  onClose:function(){
    this.setData({
      select_address: false
    });
  },
  // 选择地址
  onSelectOk:function(e){
    var detail=e.detail.values;
    this.data.province = detail[0]['name'];
    this.data.city = detail[1]['name'];
    this.data.area = detail[2]['name'];
    let area_address=detail[0]['name']+'-' + detail[1]['name']+'-' + detail[2]['name'];
    this.setData({area_address})
    this.onClose();
  },
  // 提交订单
  formSubmit:function(e){
    if (this.data.submit_flag) {   //防止重复提交
      return;
    }
    var value=e.detail.value;
    if (value.area_address.length<=0){
      wx.showToast({
        title: '请选择地区', icon:'none'
      })
      return ;
    }
    /*第一步：验证手机号码*/
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;// 判断手机号码的正则
    if (value.mobile.length <= 0) {
      wx.showToast({
        title: '请填写手机号', icon: 'none'
      })
      return;
    }

    if (value.mobile.length < 11) {
      wx.showToast({ title: '手机号码长度有误！', icon: 'none' })
      return;
    }

    if (!myreg.test(value.mobile)) {
      wx.showToast({ title: '手机号码格式错误！', icon: 'none' })
      return;
    }
    
    if (value.address.length <= 0) {
      wx.showToast({
        title: '请填写详细地址', icon: 'none'
      })
      return ;
    }

    var data={
      province: this.data.province,
      city:this.data.city,
      area:this.data.area,
      address:value.address,
      mobile:value.mobile,
      username:value.username,
      id:this.data.id,
      'number':this.data.number,
    };
    if(this.data.orderNo){
      data.orderNo=this.data.orderNo
    }
    this.data.submit_flag = true;
    model.setIntegralOrder(data,(res)=>{
      var statusCode=res.statusCode;
      if (statusCode==200){
        wx.showToast({
          title: res.data.msg, icon:'none'
        });
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/integral/index',
          });
        },2000);
      }else{
        wx.showToast({
          title: res.data.msg, icon: 'none'
        });
      }
    })
  }
})